package com.sunwayhorizo.health.common;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.sunwayhorizo.health.hl7.common.Hl7Exception;
import com.sunwayhorizo.health.hl7.datatypes.AD;
import com.sunwayhorizo.health.hl7.datatypes.ADXP;
import com.sunwayhorizo.health.hl7.datatypes.BAG_AD;
import com.sunwayhorizo.health.hl7.datatypes.BAG_EN;
import com.sunwayhorizo.health.hl7.datatypes.BAG_TEL;
import com.sunwayhorizo.health.hl7.datatypes.BL;
import com.sunwayhorizo.health.hl7.datatypes.CD;
import com.sunwayhorizo.health.hl7.datatypes.CE;
import com.sunwayhorizo.health.hl7.datatypes.CS;
import com.sunwayhorizo.health.hl7.datatypes.ED;
import com.sunwayhorizo.health.hl7.datatypes.EN;
import com.sunwayhorizo.health.hl7.datatypes.ENXP;
import com.sunwayhorizo.health.hl7.datatypes.GTS;
import com.sunwayhorizo.health.hl7.datatypes.II;
import com.sunwayhorizo.health.hl7.datatypes.INT;
import com.sunwayhorizo.health.hl7.datatypes.IVL_INT;
import com.sunwayhorizo.health.hl7.datatypes.IVL_PQ;
import com.sunwayhorizo.health.hl7.datatypes.IVL_TS;
import com.sunwayhorizo.health.hl7.datatypes.MO;
import com.sunwayhorizo.health.hl7.datatypes.ON;
import com.sunwayhorizo.health.hl7.datatypes.PQ;
import com.sunwayhorizo.health.hl7.datatypes.REAL;
import com.sunwayhorizo.health.hl7.datatypes.RTO_PQ;
import com.sunwayhorizo.health.hl7.datatypes.RTO_MO_PQ;
import com.sunwayhorizo.health.hl7.datatypes.SC;
import com.sunwayhorizo.health.hl7.datatypes.SET_CE;
import com.sunwayhorizo.health.hl7.datatypes.SET_CS;
import com.sunwayhorizo.health.hl7.datatypes.SET_II;
import com.sunwayhorizo.health.hl7.datatypes.SET_PQ;
import com.sunwayhorizo.health.hl7.datatypes.ST;
import com.sunwayhorizo.health.hl7.datatypes.TEL;
import com.sunwayhorizo.health.hl7.datatypes.TS;
import com.sunwayhorizo.health.hl7.factories.DataTypeFactory;
import com.sunwayhorizo.health.hl7.persistent.RimCoreActCdSets;
import com.sunwayhorizo.health.hl7.persistent.RimCoreEntyCdSets;
import com.sunwayhorizo.health.hl7.persistent.RimCoreEntyNames;
import com.sunwayhorizo.health.hl7.persistent.RimCoreRoleCdSets;
import com.sunwayhorizo.health.hl7.vocabulary.EntityNameUse;
import com.sunwayhorizo.health.hl7.vocabulary.NullFlavor;

public class DataTypeUtil
{
    // protected static ServiceLocator i_objServiceLocator ;
    protected static DataTypeFactory dataTypeFactory = DataTypeFactory.getInstance();


    public DataTypeUtil() throws Hl7Exception
    {
    }


    public static void initialize() throws Hl7Exception
    {
        // i_objServiceLocator = SessionManagement.getServiceLocator();
        dataTypeFactory = DataTypeFactory.getInstance();
    }


    /**
     * 把Sting类型的数据转换成HL7 BL 类型
     * 
     * @param s
     * @return
     * @throws Hl7Exception
     */
    public static BL getBL(String s) throws Hl7Exception
    {
        return dataTypeFactory.newBL(s);
    }


    /**
     * 把Sting类型的数据转换成HL7 ST 类型
     * 
     * @param s
     * @return
     * @throws Hl7Exception
     */
    public static ST getST(String s) throws Hl7Exception
    {
        return dataTypeFactory.newST(s);
    }


    /**
     * 生成HL7 SC 类型数据，参数CE为HL7的数据类型，详细请参考HL7。
     * 
     * @param s
     * @param ce
     * @return
     * @throws Hl7Exception
     */
    public static SC getSC(String s, CE ce) throws Hl7Exception
    {
        return dataTypeFactory.newSC(s, ce);
    }


    /**
     * code 为术语代码，codeSystemName为术语字典名，例如性别字典（AdministrativeGender）里的 1（男） 代码。
     * 
     * @param code
     * @param codeSystemName
     * @return
     * @throws Hl7Exception
     */
    public static CD getCD(String code, String codeSystemName) throws Hl7Exception
    {
        return dataTypeFactory.newCD(dataTypeFactory.newST(code), dataTypeFactory.nullUID(NullFlavor.NI),

        dataTypeFactory.newST(codeSystemName), dataTypeFactory.nullST(NullFlavor.NI), dataTypeFactory.nullED(NullFlavor.NI));

    }


    public static CD getCD(String code) throws Hl7Exception
    {
        return dataTypeFactory.newCD(code);

    }


    /**
     * 通过编码得到CE
     */
    public static CE getCE(String code) throws Hl7Exception
    {
        return dataTypeFactory.newCE(code);
    }


    /**
     * 参数说明同getCD。 默认情况下 codeSystemName 企业术语英文名codeSystemEnName added by ：
     * lordtan
     */
    public static CE getCE(String code, String codeSystemName) throws Hl7Exception
    {
        return dataTypeFactory.newCE(code, null, codeSystemName, null, null, null);
        // return dataTypeFactory.newCE(dataTypeFactory.newST(code),
        // dataTypeFactory.nullUID(NullFlavor.NI),
        // dataTypeFactory.newST(codeSystemName),
        // dataTypeFactory.nullST(NullFlavor.NI),
        // dataTypeFactory.nullED(NullFlavor.NI));
    }


    /**
     * code为术语代码,codeSystem为术语字典OID,codeSystemName术语字典名称,codeSystemVersion术语字典版本
     * 。 默认情况下 codeSystemName 企业术语英文名codeSystemEnName added by ： lordtan
     * 
     * @param code
     * @param codeSystem
     * @param codeSystemName
     * @param codeSystemVersion
     * @return
     * @throws Hl7Exception
     */
    public static CE getCE(String code, String codeSystem, String codeSystemName, String codeSystemVersion) throws

    Hl7Exception
    {
        return dataTypeFactory.newCE(dataTypeFactory.newST(code), dataTypeFactory.newUID(codeSystem), dataTypeFactory.newST

        (codeSystemName), dataTypeFactory.newST(codeSystemVersion), dataTypeFactory.nullED(NullFlavor.NI));
    }


    /**
     * 同上，增加了codeSystemChName术语字典中文名。
     * 
     * @param code
     * @param codeSystem
     * @param codeSystemEnName
     * @param codeSystemChName
     * @param codeName
     * @param codeSystemVersion
     * @return
     * @throws Hl7Exception
     */
    public static CE getCE(String code, String codeSystem, String codeSystemEnName, String codeSystemChName, String codeName,

    String codeSystemVersion) throws Hl7Exception
    {
        return dataTypeFactory.newCE(code, codeSystem, codeSystemEnName, codeSystemChName, codeName, codeSystemVersion);
        /*
         * return dataTypeFactory.newCE(dataTypeFactory.newST(code),
         * dataTypeFactory.newUID(codeSystem),
         * dataTypeFactory.newST(codeSystemEnName),
         * dataTypeFactory.newST(codeSystemChName),
         * dataTypeFactory.newST(codeName),
         * dataTypeFactory.newST(codeSystemVersion),
         * dataTypeFactory.nullED(NullFlavor.NI) );
         */
    }


    /**
     * 把Sting类型的数据转换成HL7 CS 类型
     * 
     * @param code
     * @return
     * @throws Hl7Exception
     */
    public static CS getCS(String code) throws Hl7Exception
    {
        return dataTypeFactory.newCS(code);
    }


    /**
     * value 为姓名的partname； type 为partname的类型（姓，名）取值于 HL7 Vocabulary
     * EntityNamePartType； useCode 为用途（法律上有效名，小名，昵称，办公室用名）取值于 HL7 Vocabulary
     * EntityNameUse。
     */
    public static BAG_EN getBagEN(String[] value, String[] type, String useCode) throws Hl7Exception
    {
        ENXP[] partname = new ENXP[value.length];
        String st = new String();
        for (int i = 0; i < value.length; i++)
        {
            partname[i] = dataTypeFactory.newENXP(value[i], dataTypeFactory.newCS(type[i]), null);
            st = st + value[i];
        }
        SET_CS nameUseCode = dataTypeFactory.newSET_CS(dataTypeFactory.newCS(useCode));
        IVL_TS nullFlavorIVL_TS = dataTypeFactory.nullIVL_TS(NullFlavor.NI);
        EN en = dataTypeFactory.newEN(partname, nameUseCode, nullFlavorIVL_TS, dataTypeFactory.newST(st));
        return dataTypeFactory.newBAG_EN(en);
    }


    /**
     * 同getBagEN(String[] value, String[] type, String useCode)，值是二维数组。
     * 
     * @param value
     * @param type
     * @param useCode
     * @return
     * @throws Hl7Exception
     */
    public static BAG_EN getBagEN(String[][] value, String[][] type, String useCode[]) throws Hl7Exception
    {
        EN name[] = new EN[value.length];
        String st = new String();
        for (int i = 0; i < value.length; i++)
        {
            ENXP[] partname = new ENXP[value[i].length];
            for (int j = 0; j < value[i].length; j++)
            {
                partname[j] = dataTypeFactory.newENXP(value[i][j], dataTypeFactory.newCS(type[i][j]), null);
                st = st + value[i][j];
            }
            name[i] = dataTypeFactory.newEN(partname, dataTypeFactory.newSET_CS(dataTypeFactory.newCS(useCode[i])),
                dataTypeFactory.nullIVL_TS(NullFlavor.NI), dataTypeFactory.newST(st));
            st = "";
        }

        return dataTypeFactory.newBAG_EN(name);
    }


    /**
     * 废弃
     * 
     * @param a_objLongUseCode
     * @param a_objLongName
     * @param a_objShortUseCode
     * @param shortName
     * @return
     * @throws Hl7Exception
     */
    public static BAG_EN getBAG_EN(String a_objLongUseCode, String a_objLongName, String a_objShortUseCode, String shortName)

    throws Hl7Exception
    {
        SET_CS l_objNullFlavorSET_CS = dataTypeFactory.nullSET_CS(dataTypeFactory.nullCS(NullFlavor.NI));
        CS l_objNullFlavorCS = dataTypeFactory.nullCS(NullFlavor.NI);
        ENXP l_objNamePart1 = dataTypeFactory.newENXP(a_objLongName, l_objNullFlavorCS, l_objNullFlavorSET_CS);
        ENXP l_objNamePart2 = dataTypeFactory.newENXP(shortName, l_objNullFlavorCS, l_objNullFlavorSET_CS);
        SET_CS l_objNameUseCode1 = dataTypeFactory.newSET_CS(dataTypeFactory.newCS(a_objLongUseCode));
        SET_CS l_objNameUseCode2 = dataTypeFactory.newSET_CS(dataTypeFactory.newCS(a_objShortUseCode));
        IVL_TS l_objNullFlavorIVL_TS = dataTypeFactory.nullIVL_TS(NullFlavor.NI);
        ON l_objNameON1 = dataTypeFactory.newON(new ENXP[] { l_objNamePart1 }, l_objNameUseCode1, l_objNullFlavorIVL_TS);
        ON l_objNameON2 = dataTypeFactory.newON(new ENXP[] { l_objNamePart2 }, l_objNameUseCode2, l_objNullFlavorIVL_TS);
        BAG_EN l_objBag_en = dataTypeFactory.newBAG_EN(new ON[] { l_objNameON1, l_objNameON2 });
        return l_objBag_en;
    }


    /**
     * value 为地址的partname； type 为partname类型（省、市、区、街道）取值于HL7 Vocabulary
     * EntyAddrPartType； useCode 为用途（家、公司、等等）取值于HL7 Vocabulary EntyAddrUses。
     * 
     */
    public static AD getAD(String[] value, String[] type, String useCode) throws Hl7Exception
    {
        ADXP[] addrPart = new ADXP[value.length];
        String st = new String();
        for (int i = 0; i < value.length; i++)
        {
            addrPart[i] = dataTypeFactory.newADXP(value[i], dataTypeFactory.newCS(type[i]));
            if ((String) value[i] != null && !((String) value[i]).equals(""))
            {
                st = st + (String) value[i];
            }
        }
        return dataTypeFactory.newAD(addrPart, dataTypeFactory.newSET_CS(dataTypeFactory.newCS(useCode)),

        dataTypeFactory.nullGTS(NullFlavor.NI), dataTypeFactory.newST(st));
    }


    /**
     * 
     * @param _extension
     * @param iiSouce
     * @param iiState
     * @param iiType
     * @param identifierName
     * @return
     * @throws Hl7Exception
     */
    public static SET_II getSet_II(String _extension, String iiSouce, String iiState, CE iiType, String identifierName) throws Hl7Exception
    {
        II ii = dataTypeFactory.newII(dataTypeFactory.newUID("192.168.1.1"), dataTypeFactory.newST(_extension), dataTypeFactory.newST(identifierName),
            iiType, dataTypeFactory.newST(iiSouce), dataTypeFactory.newST(iiState));

        return dataTypeFactory.newSET_II(ii);
    }


    /**
     * 把AD类型生成BagAD类型。
     * 
     * @param ag
     * @return
     * @throws Hl7Exception
     */
    public static BAG_AD getBagAD(AD ag) throws Hl7Exception
    {
        return dataTypeFactory.newBAG_AD(ag);
    }


    /**
     * 把Sting类型的数据转换成HL7 ED 类型
     * 
     * @param desc
     * @return
     * @throws Hl7Exception
     */
    public static ED getED(String desc) throws Hl7Exception
    {
        return dataTypeFactory.newED(desc, dataTypeFactory.newCS("ST"), dataTypeFactory.nullCS(NullFlavor.NI),

        dataTypeFactory.nullCS(NullFlavor.NI), dataTypeFactory.nullCS(NullFlavor.NI), dataTypeFactory.nullTEL(NullFlavor.NI), null,
            dataTypeFactory.nullCS

            (NullFlavor.NI), null, dataTypeFactory.nullCS(NullFlavor.NI));

    }


    /**
     * descText 为文本文件，descByte为二进制文件。
     * 
     * @param descText
     * @param descByte
     * @return
     * @throws Hl7Exception
     */
    public static ED getED(String descText, byte[] descByte) throws Hl7Exception
    {
        if (descByte != null && descByte.length > 0)
        {
            return dataTypeFactory.newED(descText, dataTypeFactory.newCS("ST"), dataTypeFactory.nullCS(NullFlavor.NI),
                dataTypeFactory.nullCS(NullFlavor.NI), dataTypeFactory.nullCS(NullFlavor.NI), dataTypeFactory.nullTEL

                (NullFlavor.NI), dataTypeFactory.newBIN(descByte), dataTypeFactory.nullCS(NullFlavor.NI), null, dataTypeFactory.nullCS

                (NullFlavor.NI));

        }
        else
        {
            return dataTypeFactory.newED(descText, dataTypeFactory.newCS("ST"), dataTypeFactory.nullCS(NullFlavor.NI),
                dataTypeFactory.nullCS(NullFlavor.NI), dataTypeFactory.nullCS(NullFlavor.NI), dataTypeFactory.nullTEL

                (NullFlavor.NI), dataTypeFactory.nullBIN(dataTypeFactory.nullCS(NullFlavor.NI)), dataTypeFactory.nullCS(NullFlavor.NI), null,
                dataTypeFactory.nullCS(NullFlavor.NI));
        }
    }


    /**
     * address: 通信地址名称； scheme：通信地址类型,取值于 HL7 Vocabulary
     * UrlScheme。例如：fax、file、ftp、http、mailto、tel、telnet； useablePeriod：有效时间范围
     * 例如电话8:00-17:00; useCode: 用途，取值于 HL7 Vocabulary
     * EntityTelUse。例如：H-家庭通信地址、WP-办公室通信地址、TMP-临时通信地址、EC-紧急联系地址、MC-移动
     * 
     * @param address
     * @param scheme
     * @param useablePeriod
     * @param useCode
     * @return
     * @throws Hl7Exception
     */
    public static BAG_TEL getBAG_TEL(String address[], String scheme[], Date useablePeriod[][], String useCode[]) throws

    Hl7Exception
    {
        TEL tel[] = new TEL[scheme.length];
        for (int i = 0; i < scheme.length; i++)
        {
            tel[i] = dataTypeFactory.newTEL(dataTypeFactory.newCS(scheme[i]), dataTypeFactory.newST(address[i]),
                dataTypeFactory.newGTS(getIVL_TS(useablePeriod[i][0], useablePeriod[i][1], "", "", null)),
                dataTypeFactory.newSET_CS(dataTypeFactory.newCS(useCode[i])));
        }

        return dataTypeFactory.newBAG_TEL(tel);
    }


    /**
     * Date 类型转换成 HL7 TS 类型。
     * 
     * @param date
     * @return
     * @throws Hl7Exception
     */
    public static TS getTS(Date date) throws Hl7Exception
    {

        return dataTypeFactory.newTS(date);
    }


    /**
     * String 类型转换成 HL7 TS 类型。
     * 
     * @param date
     * @return
     * @throws Hl7Exception
     */
    public static TS getTS(String date) throws Hl7Exception
    {
        if (StrUtil.isEmpty(date))
        {
            return dataTypeFactory.newTS("");
        }
        Date target = null;
        DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        DateFormat df2 = new SimpleDateFormat("yyyyMMddHHmmss");

        DateFormat df3 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

        DateFormat df4 = new SimpleDateFormat("yyyyMMdd");

        if (date.length() == 10 || date.length() == 8)
        {
            try
            {
                target = df4.parse(StrUtil.formatStr(date));
            }
            catch (ParseException e)
            {
                e.printStackTrace();
                return dataTypeFactory.newTS("");
            }
            return dataTypeFactory.newTS(target);
        }

        if (date.length() == 14 && date.charAt(4) != '/' && date.charAt(4) != '-')
        {
            try
            {
                target = df2.parse(date);

            }
            catch (ParseException e)
            {
                e.printStackTrace();
                return dataTypeFactory.newTS("");
            }
            return dataTypeFactory.newTS(target);
        }

        if (date.length() == 19 && date.charAt(4) == '-')
        {
            try
            {
                target = df1.parse(date);

            }
            catch (ParseException e)
            {
                e.printStackTrace();
                return dataTypeFactory.newTS("");
            }
            return dataTypeFactory.newTS(target);
        }
        else
        {
            try
            {
                target = df3.parse(date);

            }
            catch (ParseException e)
            {
                e.printStackTrace();
                return dataTypeFactory.newTS("");
            }
            return dataTypeFactory.newTS(target);
        }

    }


    /**
     * 把 HL7 IVL_TS 类型转换成 HL7 GTS 类型 IVL_TS：为时间区间。
     */
    public static GTS getGTS(IVL_TS ivl_ts) throws Hl7Exception
    {
        return dataTypeFactory.newGTS(ivl_ts);
    }


    /**
     * 生成 HL7 IVL_TS 类型 startDate：开始时间； endDate：结束时间。
     */
    public static IVL_TS getIVL_TS(Date startDate, Date endDate) throws Hl7Exception
    {
        return dataTypeFactory.newIVL_TS(dataTypeFactory.newTS(startDate), dataTypeFactory.newBL("TRUE"),

        dataTypeFactory.newTS(endDate), dataTypeFactory.newBL("FALSE"));
    }


    /**
     * 生成 HL7 IVL_TS 类型 startDate：开始时间； endDate：结束时间 value：开始时间与结束时间 间的间隔；
     * unit：间隔单位，例如小时。 lastDate： 区间增加时间，例如开始时间-扩展时间、结束时间+扩展时间。
     */
    public static IVL_TS getIVL_TS(Date startDate, Date endDate, String value, String unit, Date lastDate) throws Hl7Exception
    {

        return dataTypeFactory.newIVL_TS(dataTypeFactory.newTS(startDate), dataTypeFactory.newBL("TRUE"),

        dataTypeFactory.newTS(endDate), dataTypeFactory.newBL("FALSE"), dataTypeFactory.newPQ(unit, value), dataTypeFactory.newTS(lastDate));
    }


//    public static IVL_TS getIVL_TS(Date startDate, Date endDate) throws Hl7Exception
//    {
//
//        return dataTypeFactory.newIVL_TS(dataTypeFactory.newTS(startDate),
//
//        dataTypeFactory.newTS(endDate));
//    }


    /**
     * Long 型转换成 HL7 INT 类型
     */
    public static INT getINT(Long lowValueNum) throws Hl7Exception
    {
        if (lowValueNum == null)
        {
            lowValueNum = 0l;
        }
        return dataTypeFactory.newINT(lowValueNum);
    }


    /**
     * 生成 HL7 IVL_INT 类型 lowValueNum：下限； lowInclFlag：下限标识； highValueNum：上限；
     * highInclFlag：上限标识。
     */
    public static IVL_INT getIVL_INT(Long lowValueNum, String lowInclFlag, Long highValueNum, String highInclFlag) throws

    Hl7Exception
    {
        return dataTypeFactory.newIVL_INT(dataTypeFactory.newINT(lowValueNum), dataTypeFactory.newBL(lowInclFlag),

        dataTypeFactory.newINT(highValueNum), dataTypeFactory.newBL(highInclFlag));
    }


    /**
     * 生成 HL7 IVL_INT
     * 
     * @param center
     * @return
     * @throws Hl7Exception
     */
    public static IVL_INT getIVL_INT(Long center) throws Hl7Exception
    {
        return dataTypeFactory.newIVL_INT(dataTypeFactory.newINT(center));
    }


    /**
     * 生成 HL7 IVL_PQ
     * 
     * @param value
     *            用量
     * @param unit
     *            用量单位
     * @return IVL_PQ
     * @throws Hl7Exception
     */
    public static IVL_PQ getIVL_PQ(String value, String unit) throws Hl7Exception
    {
        return dataTypeFactory.newIVL_PQ(dataTypeFactory.newPQ(value, unit));
    }


    /*
     * public static InfrastructureRoot[]
     * getFetchedResultsFromQueryResult(ControlAct a_objQueryResultCtrlAct,
     * CoreFetch a_objCoreFetch) throws CTBException {
     * 
     * Iterator l_objMainQueryOBControlActRelationships =
     * a_objQueryResultCtrlAct
     * .getOBActRelationships((QueryComponentFactory.getInstance
     * (i_objServiceLocator).newActRelationshipFetch())); QueryAct
     * l_objQueryResultAct = null; while
     * (l_objMainQueryOBControlActRelationships.hasNext() && l_objQueryResultAct
     * == null) { ActRelationship l_objQuryCurrentActRelationship =
     * (ActRelationship) l_objMainQueryOBControlActRelationships.next(); Act
     * targetAct =
     * l_objQuryCurrentActRelationship.getTarget((QueryComponentFactory
     * .getInstance(i_objServiceLocator).newActFetch())); if (targetAct
     * instanceof QueryAct) { l_objQueryResultAct = (QueryAct) targetAct; } }
     * 
     * List l_objQueryResultsList = new ArrayList(); Iterator
     * l_objOBQueryActRelationships =
     * l_objQueryResultAct.getOBActRelationships((
     * QueryComponentFactory.getInstance
     * (i_objServiceLocator)).newActRelationshipFetch()); while
     * (l_objOBQueryActRelationships != null &&
     * l_objOBQueryActRelationships.hasNext()) {
     * 
     * ActRelationship l_objCurrentActRelationship = (ActRelationship)
     * l_objOBQueryActRelationships.next();
     * //System.out.println("l_objCurrentActRelationship****"+
     * l_objCurrentActRelationship); if (l_objCurrentActRelationship != null) {
     * if (a_objCoreFetch instanceof ActFetch) {
     * l_objQueryResultsList.add(getAct(l_objCurrentActRelationship, (ActFetch)
     * a_objCoreFetch)); } else if (a_objCoreFetch instanceof
     * ActRelationshipFetch) {
     * l_objQueryResultsList.add(getActRelationship(l_objCurrentActRelationship
     * ,(ActRelationshipFetch) a_objCoreFetch)); } else if (a_objCoreFetch
     * instanceof ParticipationFetch) {
     * l_objQueryResultsList.add(getParticipation
     * (l_objCurrentActRelationship,(ParticipationFetch) a_objCoreFetch)); }
     * else if (a_objCoreFetch instanceof RoleFetch) {
     * l_objQueryResultsList.add(getRole(l_objCurrentActRelationship,(RoleFetch)
     * a_objCoreFetch)); }
     * 
     * if (a_objCoreFetch instanceof EntityFetch) {
     * l_objQueryResultsList.add(getEntity
     * (l_objCurrentActRelationship,(EntityFetch) a_objCoreFetch)); } } } return
     * (InfrastructureRoot[]) l_objQueryResultsList.toArray(new
     * InfrastructureRoot[l_objQueryResultsList.size()]); }
     * 
     * 
     * public static Act getAct(ActRelationship actRelationship, ActFetch fetch)
     * throws CTBException { return actRelationship.getTarget(fetch); }
     * 
     * public static ActRelationship getActRelationship(ActRelationship
     * actRelationship, ActRelationshipFetch fetch) throws CTBException { Act
     * pseudoAct = getAct(actRelationship,
     * (QueryComponentFactory.getInstance(i_objServiceLocator)).newActFetch());
     * Iterator obActRelationships = pseudoAct.getOBActRelationships(fetch);
     * return (ActRelationship) obActRelationships.next(); }
     * 
     * public static Participation getParticipation(ActRelationship
     * actRelationship, ParticipationFetch fetch) throws CTBException {
     * //System.out.println("Inside getParticipation"); Act pseudoAct =
     * getAct(actRelationship,
     * (QueryComponentFactory.getInstance(i_objServiceLocator)).newActFetch());
     * Iterator participations = pseudoAct.getParticipations(fetch);
     * //System.out.println("Finish getParticipation "+
     * participations.hasNext()); return (Participation) participations.next();
     * }
     * 
     * public static Role getRole(ActRelationship actRelationship, RoleFetch
     * fetch) throws CTBException { Participation pseudoParticipation =
     * getParticipation
     * (actRelationship,(QueryComponentFactory.getInstance(i_objServiceLocator
     * )).newParticipationFetch()); return pseudoParticipation.getRole(fetch); }
     * 
     * public static Entity getEntity(ActRelationship actRelationship,
     * EntityFetch fetch) throws CTBException { Role pseudoRole =
     * getRole(actRelationship,
     * (QueryComponentFactory.getInstance(i_objServiceLocator)).newRoleFetch());
     * return pseudoRole.getPlayerEntity(fetch); }
     * 
     * 
     * public static EntityFetch createEntityFetch(EntityAttributeCriteria
     * a_objentityAttributeCriteria) { //System.out.println("****"+
     * i_objServiceLocator); EntityFetch l_objEntityFetch =
     * (QueryComponentFactory.getInstance(i_objServiceLocator)).newEntityFetch(
     * a_objentityAttributeCriteria); l_objEntityFetch.retrieveAll();
     * //l_objEntityFetch.retrieveId(true);
     * 
     * return l_objEntityFetch; }
     * 
     * public static ActFetch createActFetch(ActAttributeCriteria
     * a_objActAttributeCriteria) { ActFetch l_objActFetch =
     * (QueryComponentFactory
     * .getInstance(i_objServiceLocator)).newActFetch(a_objActAttributeCriteria
     * ); l_objActFetch.retrieveAll(); //l_objActFetch.retrieveMoodCode(true);
     * //l_objActFetch.retrieveClassCode(true);
     * //l_objActFetch.retrieveStatusCode(true); return l_objActFetch; }
     * 
     * public static RoleFetch createRoleFetch(RoleAttributeCriteria
     * a_objRoleAttributeCriteria) { RoleFetch l_objRoleFetch =
     * (QueryComponentFactory
     * .getInstance(i_objServiceLocator)).newRoleFetch(a_objRoleAttributeCriteria
     * ); l_objRoleFetch.retrieveAll(); return l_objRoleFetch; }
     * 
     * public static ENCriteria getENCriteria(String a_objLongUseCode, String
     * a_objLongName, String a_objShortUseCode, String a_objShortName) throws
     * CTBException{
     * 
     * ENXPAttributeCriteria l_objEnxpAttribCriteriaLong =
     * (QueryComponentFactory
     * .getInstance(i_objServiceLocator)).newENXPAttributeCriteria();
     * l_objEnxpAttribCriteriaLong.setST(SearchOperator.EQUALS,
     * dataTypeFactory.newST(a_objLongName));
     * 
     * ENAttributeCriteria l_objEnAttribCriteriaLong =
     * (QueryComponentFactory.getInstance
     * (i_objServiceLocator)).newENAttributeCriteria();
     * l_objEnAttribCriteriaLong.setENXPCriteria(l_objEnxpAttribCriteriaLong);
     * l_objEnAttribCriteriaLong.setUse(SetSearchOperator.ALL,new CS[] {
     * dataTypeFactory.newCS(a_objLongUseCode) });
     * 
     * ENXPAttributeCriteria l_objEnxpAttribCriteriaShort =
     * (QueryComponentFactory
     * .getInstance(i_objServiceLocator)).newENXPAttributeCriteria();
     * l_objEnxpAttribCriteriaShort
     * .setST(SearchOperator.EQUALS,dataTypeFactory.newST(a_objShortName));
     * ENAttributeCriteria l_objEnAttribCriteriaShort =
     * (QueryComponentFactory.getInstance
     * (i_objServiceLocator)).newENAttributeCriteria();
     * l_objEnAttribCriteriaShort.setENXPCriteria(l_objEnxpAttribCriteriaShort);
     * l_objEnAttribCriteriaShort.setUse(SetSearchOperator.ALL,new CS[] {
     * dataTypeFactory.newCS(a_objShortUseCode) });
     * 
     * Criteria l_objFinalCriteria = null; if (a_objLongName != null &&
     * a_objShortName != null) l_objFinalCriteria =
     * (QueryComponentFactory.getInstance
     * (i_objServiceLocator)).and(l_objEnAttribCriteriaLong,
     * l_objEnAttribCriteriaShort); else if (a_objLongName != null)
     * l_objFinalCriteria = l_objEnAttribCriteriaLong; else l_objFinalCriteria =
     * l_objEnAttribCriteriaShort;
     * 
     * return (ENCriteria) l_objFinalCriteria; }
     */

    /*
     * public static AD getAddress() throws CTBException { ADXP[]
     * l_objAddressParts = new ADXP[3]; l_objAddressParts[0] =
     * getAddressPart("US","CNT"); l_objAddressParts[1] =
     * getAddressPart("2W3E4R","ZIP"); l_objAddressParts[2] =
     * getAddressPart("100 Oracle Parkway","SAL"); CS[] l_objCS = new CS[] {
     * dataTypeFactory.newCS(ModuleConstants.ORG_ADDR_USE_CODE)}; SET_CS
     * l_objAddressUseCodes = dataTypeFactory.newSET_CS(l_objCS); GTS
     * l_objNullFlavorGTS = dataTypeFactory.nullGTS(NullFlavor.NI); AD address =
     * dataTypeFactory.newAD(l_objAddressParts, l_objAddressUseCodes,
     * l_objNullFlavorGTS); return address; }
     */

    // get AddressPart
    /*
     * public static ADXP getAddressPart(String txt, String code) throws
     * Hl7Exception { return dataTypeFactory.newADXP(txt,
     * dataTypeFactory.newCS(code)); }
     * 
     * public static SET_II getSet_II(String root, String extentsion, boolean
     * flag) throws Hl7Exception { return dataTypeFactory.newSET_II(root,
     * extentsion, flag); }
     */
    /**
     * root：组织唯一标识，例如医院的唯一代码； extentsion：为系统内各个业务的唯一标识，例如员工工号，患者ID；
     * identifierName:跟root，extentsion联合做唯一性条件锁定一条记录，不能为空。 应用场景可以参考Act_II表中就诊号
     */
    public static SET_II getSet_II(String root, String extentsion, String state, String identifierName) throws Hl7Exception
    {
        return dataTypeFactory.newSET_II(root, extentsion, state, identifierName);
    }


    /**
     * code为术语代码,codeSystem为术语字典OID,codeSystemName术语字典名称,codeSystemVersion术语字典版本
     * 。 默认情况下 codeSystemName 企业术语英文名codeSystemEnName added by ： lordtan
     * 
     * @param code
     * @param codeSystem
     * @param codeSystemName
     * @param codeSystemVersion
     * @return
     * @throws Hl7Exception
     */
    public static SET_CE getSet_CE(String code, String codeSystem, String codeSystemName, String codeSystemVersion) throws

    Hl7Exception
    {
        return dataTypeFactory.newSET_CE(getCE(code, codeSystem, codeSystemName, codeSystemVersion));
    }


    /**
     * 传入CE类型的数据，生成Set_CE数据。
     */
    public static SET_CE getSet_CE(CE ce) throws Hl7Exception
    {
        return dataTypeFactory.newSET_CE(ce);
    }


    /**
     * 
     * value:数值，例如 1、3、100、等； unit：单位，例如 克、千克、等。
     */
    public static SET_PQ getSET_PQ(String value, String unit) throws Hl7Exception
    {
        return dataTypeFactory.newSET_PQ(dataTypeFactory.newPQ(dataTypeFactory.newREAL(value), dataTypeFactory.newCS(unit)));
    }


    public static void main(String[] args)
    {
    }


    /**
     * 生成HL7货币数据类型 MO
     * 
     * @author Obama
     * @param bigdecimal
     *            货币值
     * @param s
     *            货币单位
     * @return MO
     * @throws Hl7Exception
     */
    public static MO getMO(BigDecimal bigdecimal, String s) throws Hl7Exception
    {
        return dataTypeFactory.newMO(bigdecimal, s);
    }


    /**
     * 生成HL7货币数据类型 MO
     * 
     * @author Obama
     * @param bigdecimal
     *            货币值
     * @param s
     *            货币单位
     * @return MO
     * @throws Hl7Exception
     */
    public static MO getMO(double bigdecimal, String s) throws Hl7Exception
    {

        return dataTypeFactory.newMO(BigDecimal.valueOf(bigdecimal), s);
    }


    /**
     * 生成HL7货币数据类型 MO
     * 
     * @author Obama
     * @param real
     *            货币值
     * @param cs
     *            货币单位
     * @return MO
     * @throws Hl7Exception
     */
    public static MO getMO(REAL real, CS cs) throws Hl7Exception
    {
        return dataTypeFactory.newMO(real, cs);
    }


    /**
     * 生成HL7货币数据类型 MO
     * 
     * @author Obama
     * @param bigdecimal
     *            货币值
     * @param s
     *            货币单位
     * @return MO
     * @throws Hl7Exception
     */
    public static MO getMO(long bigdecimal, String s) throws Hl7Exception
    {

        return dataTypeFactory.newMO(BigDecimal.valueOf(bigdecimal), s);
    }


    /**
     * 生成HL7真实数值类型REAL
     * 
     * @author Obama
     * @param bigdecimal
     *            数值
     * 
     * @return REAL
     * @throws Hl7Exception
     */
    public static REAL getREAL(BigDecimal bigdecimal) throws Hl7Exception
    {

        return dataTypeFactory.newREAL(bigdecimal);
    }


    /**
     * 生成HL7真实数值类型REAL
     * 
     * @author Obama
     * @param bigdecimal
     *            数值字符串
     * 
     * @return REAL
     * @throws Hl7Exception
     */
    public static REAL getREAL(String bigdecimal) throws Hl7Exception
    {

        return dataTypeFactory.newREAL(bigdecimal);
    }


    /**
     * value 为数值, unit 为单位。
     * 
     * @param value
     * @param unit
     * @return
     * @throws Hl7Exception
     */
    public static PQ getPQ(String value, String unit) throws Hl7Exception
    {
        return dataTypeFactory.newPQ(dataTypeFactory.newREAL((StrUtil.isEmpty(value) ? "0" : value)),
            dataTypeFactory.newCS(StrUtil.isEmpty(unit) ? "" : unit));
    }


    public static RTO_PQ getRTO_PQ(String value1, String unit1, String value2, String unit2) throws Hl7Exception
    {
        return dataTypeFactory.newRTO_PQ(dataTypeFactory.newPQ(dataTypeFactory.newREAL(value1), dataTypeFactory.newCS(unit1)),
            dataTypeFactory.newPQ(dataTypeFactory.newREAL(value2), dataTypeFactory.newCS(unit2)));
    }


    public static RTO_PQ getRTO_PQ(String value, String unit) throws Hl7Exception
    {
        return dataTypeFactory.newRTO_PQ(dataTypeFactory.newPQ(dataTypeFactory.newREAL(value), dataTypeFactory.newCS(unit)),
            dataTypeFactory.newPQ(dataTypeFactory.newREAL(value), dataTypeFactory.newCS(unit)));
    }


    /**
     * 生成HL7RTO_MO_PO数据类型
     * 
     * @author
     * @param 数值字符串
     * 
     * @return REAL
     * @throws Hl7Exception
     */
    public static RTO_MO_PQ getRTO_MO_PQ(Double value1, String literal, String currCode, String value2, String unit2) throws Hl7Exception
    {

        return dataTypeFactory.newRTO_MO_PQ(getMO(value1, currCode), getPQ(value2, unit2));
    }


    /**
     * 生成HL7RTO_MO_PO数据类型
     * 
     * @param value
     *            单价
     * @param unit
     *            单位
     * @return RTO_MO_PQ
     * @throws Hl7Exception
     */
    public static RTO_MO_PQ getRTO_MO_PQ(String value, String unit) throws Hl7Exception
    {
        return dataTypeFactory.newRTO_MO_PQ(getMO(Double.valueOf((StrUtil.isEmpty(value) ? "0" : value)), "元"),
            getPQ("0", (StrUtil.isEmpty(unit) ? "" : unit)));
    }


    /**
     * 生成HL7RTO_MO_PO数据类型
     * 
     * @param value
     *            单价
     * @param unit
     *            单位
     * @return RTO_MO_PQ
     * @throws Hl7Exception
     */
    public static RTO_MO_PQ getRTO_MO_PQ(Double value, String unit) throws Hl7Exception
    {
        return dataTypeFactory.newRTO_MO_PQ(getMO(value, "元"), getPQ("1", unit));
    }


    /**
     * 生成HL7II
     * 
     * @author Obama
     * @param
     * 
     * 
     * @return II
     * @throws Hl7Exception
     */
    public static II getII(String root, String extension, String iiState, String identifierName) throws Hl7Exception
    {

        return dataTypeFactory.newII(root, extension, iiState, identifierName);
    }


    /**
     * 生成HL7II
     * 
     * @author Obama
     * @param
     * 
     * 
     * @return II
     * @throws Hl7Exception
     */
    public static II getII(String root, String extension, String identifierName) throws Hl7Exception
    {

        return dataTypeFactory.newII(root, extension, null, identifierName);
    }


    public static II getII(String root, String extension, String iiState, String identifierName, String clinicNo) throws Hl7Exception
    {
        return dataTypeFactory.newII(root, extension, iiState, identifierName, clinicNo);
    }


    public static II getII(String ii, String iiSouce, String state, CE iiType, String identifierName) throws Hl7Exception
    {
        II iI = dataTypeFactory.newII(dataTypeFactory.newUID("192.168.1.1"), dataTypeFactory.newST(ii), dataTypeFactory.newST(identifierName), iiType,
            dataTypeFactory.newST(iiSouce), dataTypeFactory.newST(state));
        return iI;
    }


    public static CE getCE(RimCoreRoleCdSets cdsets)
    {
        if (cdsets == null)
        {
            return null;
        }
        return dataTypeFactory.newCE(cdsets.getConceptCode(), cdsets.getCoercedConceptId(), cdsets.getCodingschemeEnName(),
            cdsets.getCodingschemeChName(), cdsets.getConceptName(), cdsets.getCodingschemeVersionName());
    }


    public static CE getCE(RimCoreActCdSets cdsets)
    {
        if (cdsets == null)
            return null;
        return dataTypeFactory.newCE(cdsets.getConceptCode(), cdsets.getCoercedConceptId(), cdsets.getCodingschemeEnName(),
            cdsets.getCodingschemeChName(), cdsets.getConceptName(), cdsets.getCodingschemeVersionName());
    }


    public static CE getCE(RimCoreEntyCdSets cdsets)
    {
        if (cdsets == null)
            return null;
        return dataTypeFactory.newCE(cdsets.getConceptCode(), cdsets.getCoercedConceptId(), cdsets.getCodingschemeEnName(),
            cdsets.getCodingschemeChName(), cdsets.getConceptName(), cdsets.getCodingschemeVersionName());
    }


    /**
     * 把Sting类型的数据转换成HL7 BL 类型
     * 
     * @param s
     * @return
     * @throws Hl7Exception
     */
    public static EN getEN(ST name, CS use) throws Hl7Exception
    {
        return dataTypeFactory.newEN(name, use);
    }
}
